﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Enterprise.Management.ViewModels;
using Enterprise.Management.Data;
using Enterprise.Management.Business;
using Enterprise.Management.Caches;

namespace Enterprise.Management.Controllers
{
    public class DependencyBranchController : BaseController
    {
        DependencyBranchBLL bllDependencyBranch = new DependencyBranchBLL();
        OperationHistoryBLL bllOperationHistory = new OperationHistoryBLL();

        public ActionResult Index()
        {
            DP_Local_Info dp_local_info = bllDependencyBranch.Get(BranchCategoryCache.BranchCategoryId.Value);
            DependencyBranchViewModel vmDependencyBranch = new DependencyBranchViewModel();

            if (dp_local_info != null)
            {
                vmDependencyBranch.isNew = false;
                vmDependencyBranch.dp_local_info = dp_local_info;
            }
            else
            {
                vmDependencyBranch.isNew = true;
            }

            return View(vmDependencyBranch);
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(EditDependencyBranchViewModel vmDependencyBranch)
        {
            if (ModelState.IsValid)
            {
                DP_Local_Info dp_local_info = new DP_Local_Info();
                dp_local_info.LclInf_CtgrID = BranchCategoryCache.BranchCategoryId.Value;
                dp_local_info.LclInf_BranchNum = vmDependencyBranch.LclInf_BranchNum;
                dp_local_info.LclInf_JiaoyiZongliang = vmDependencyBranch.LclInf_JiaoyiZongliang;
                dp_local_info.LclInf_BujunJiaoyiliang = vmDependencyBranch.LclInf_BujunJiaoyiliang;
                dp_local_info.LclInf_Yongjinlv = vmDependencyBranch.LclInf_Yongjinlv;
                dp_local_info.LclInf_ShichangZhanbi = vmDependencyBranch.LclInf_ShichangZhanbi;
                dp_local_info.ModifiedBy = HtmlCache.GetUser().User_ID;
                dp_local_info.ModifiedDate = DateTime.Now;
                DP_Operation_History dp_operation_history = new DP_Operation_History();
                dp_operation_history.OprHis_CtgrID = BranchCategoryCache.BranchCategoryId.Value;
                dp_operation_history.OprHis_TypeID = (int)FunctionType.DependencyBranch;
                dp_operation_history.OprHis_Date = DateTime.Now;
                dp_operation_history.OprHis_Comment = "新建属地信息";

                if (bllDependencyBranch.Add(dp_local_info))
                {
                    if (bllOperationHistory.AddOperationHistory(dp_operation_history))
                    {
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        ModelState.AddModelError("", "新建失败，请稍后再试！");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "新建失败，请稍后再试！");
                }
            }

            return View(vmDependencyBranch);
        }

        public ActionResult Edit()
        {
            DP_Local_Info dp_local_info = bllDependencyBranch.Get(BranchCategoryCache.BranchCategoryId.Value);
            EditDependencyBranchViewModel vmDependencyBranch = new EditDependencyBranchViewModel();
            vmDependencyBranch.LclInf_BranchNum = dp_local_info.LclInf_BranchNum;
            vmDependencyBranch.LclInf_JiaoyiZongliang = dp_local_info.LclInf_JiaoyiZongliang;
            vmDependencyBranch.LclInf_BujunJiaoyiliang = dp_local_info.LclInf_BujunJiaoyiliang;
            vmDependencyBranch.LclInf_Yongjinlv = dp_local_info.LclInf_Yongjinlv;
            vmDependencyBranch.LclInf_ShichangZhanbi = dp_local_info.LclInf_ShichangZhanbi;
            return View(vmDependencyBranch);
        }

        [HttpPost]
        public ActionResult Edit(EditDependencyBranchViewModel vmDependencyBranch)
        {
            if(ModelState.IsValid)
            {
                DP_Local_Info dp_local_info = bllDependencyBranch.Get(BranchCategoryCache.BranchCategoryId.Value);
                dp_local_info.LclInf_CtgrID = BranchCategoryCache.BranchCategoryId.Value;
                dp_local_info.LclInf_BranchNum = vmDependencyBranch.LclInf_BranchNum;
                dp_local_info.LclInf_JiaoyiZongliang = vmDependencyBranch.LclInf_JiaoyiZongliang;
                dp_local_info.LclInf_BujunJiaoyiliang = vmDependencyBranch.LclInf_BujunJiaoyiliang;
                dp_local_info.LclInf_Yongjinlv = vmDependencyBranch.LclInf_Yongjinlv;
                dp_local_info.LclInf_ShichangZhanbi = vmDependencyBranch.LclInf_ShichangZhanbi;
                dp_local_info.ModifiedBy = HtmlCache.GetUser().User_ID;
                dp_local_info.ModifiedDate = DateTime.Now;
                DP_Operation_History dp_operation_history = new DP_Operation_History();
                dp_operation_history.OprHis_CtgrID = BranchCategoryCache.BranchCategoryId.Value;
                dp_operation_history.OprHis_TypeID = (int)FunctionType.DependencyBranch;
                dp_operation_history.OprHis_Date = DateTime.Now;
                dp_operation_history.OprHis_Comment = "更新属地信息";

                if (bllDependencyBranch.Update(dp_local_info))
                {
                    if (bllOperationHistory.AddOperationHistory(dp_operation_history))
                    {
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        ModelState.AddModelError("", "更新失败，请稍后再试！");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "更新失败，请稍后再试！");
                }
            }

            return View(vmDependencyBranch);
        }
    }
}
